import 'package:flutter/material.dart';
import '../../model/security_check.dart';
import '../../pages/security/the_checkbox.dart';
import '../../pages/station/station_helper.dart';
import '../../model/station.dart';

import '../../widgets/IconWidget.dart';
import '../../widgets/inputWidget.dart';
import '../../helper/Helper.dart';
import '../../helper/global_config.dart';
import '../../helper/uidata.dart';
import '../../model/report.dart';
import 'security_helper.dart';

class SecurityCheckAddPage extends StatefulWidget {
  @override
  SecurityCheckAddPage(this.stc1, this.added);
  final SecurityCheck stc1;
  final VoidCallback added;
  @override
  SecurityCheckAddPageState createState() => new SecurityCheckAddPageState();
}

class SecurityCheckAddPageState extends State<SecurityCheckAddPage> {
  late SecurityCheck X1;

  final TextEditingController c1 = new TextEditingController();
  final TextEditingController c2 = new TextEditingController();
  final TextEditingController c3 = new TextEditingController();

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    if (widget.stc1 == null) {
      var now = Helper.DateTimeNowString();
      X1 = SecurityCheck(
          0,
          Helper.GetGuid(),
          UIData.CurrentUser!.Guid,
          0,
          now,
          "定期检查",
          "标题",
          UIData.CurrentUser!.Name,
          "成都办公区域",
          "消防安全",
          "",
          "发现问题...",
          "处置方式...",
          "",
          "",
          now,
          "",
          "",
          "");
    } else {
      X1 = widget.stc1;
    }

    c1.text = X1.Members;
    c2.text = X1.Problems;
    c3.text = X1.Works;
  }

  upload(BuildContext context) async {
    X1.Members = c1.text;
    X1.Problems = c2.text;
    X1.Works = c3.text;
    X1.Title = X1.Region + X1.SType;
    if (X1.ID == 0) {
      await SecurityCheck.Add(X1);
    } else {
      await SecurityCheck.Update(X1);
    }
    // widget.added();
    Navigator.of(context).pop();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      //backgroundColor: Colors.white,
      appBar: AppBar(
        title: Text("安全检查记录"),
      ),
      body: SecurityCheckAddBody(context),
    );
  }

  void onTypeChange(dynamic nn) {
    String yy = nn.toString();

    setState(() {
      X1.SType = yy;
    });

    //print(yy);
  }

  void onRegionChange(dynamic nn) {
    String yy = nn.toString();

    setState(() {
      X1.Region = yy;
    });

    //print(yy);
  }

  void onContentChange(String nn) {
    print(nn);
    setState(() {
      X1.CheckItems = nn;
    });
  }

  void onEQMChange(String nn) {
    String yy = nn.toString();
    setState(() {
      X1.ExamQM = yy;
    });
  }

  SecurityCheckAddBody(BuildContext context) => SingleChildScrollView(
        child: SecurityCheckAddFields(context),
      );

  List<Widget> getWidgetList(BuildContext context) {
    List<Widget> list1 = <Widget>[];
    List<Widget> list2 = <Widget>[];
    List<Widget> list3 = <Widget>[];
    List<Widget> list4 = <Widget>[];

    list1.add(InputWidget.inCard(
      ListTile(
        leading: IconWidget.CheckImage(),
        title: Text("创建检查记录单"),
      ),
    ));

    list2.add(
      InputWidget.getSelect("检查类型", SecurityHelper.STypeList(), X1.SType,
          onChanged: onTypeChange),
    );

    list2.add(Divider(
      height: 0.0,
    ));
    list2.add(
      InputWidget.getSelect("检查区域", SecurityHelper.RegionList(), X1.Region,
          onChanged: onRegionChange),
    );
    list2.add(Divider(
      height: 0.0,
    ));

    list2.add(
      TheCheckBoxPage(
        "检查项目",
        SecurityHelper.ContentList(X1.Region),
        X1.CheckItems,
        onContentChange,
        col: Colors.blue,
      ),
    );

    list2.add(Divider(
      height: 0.0,
    ));

    list2.add(InputWidget.getInput2("检查人员", c1));
    list2.add(Divider(
      height: 0.0,
    ));

    list1.add(InputWidget.inCard2(list2));

    list1.add(
        InputWidget.inCard(InputWidget.getButton("创建", () => upload(context))));

    //list1.add(getCLWidget(s1.State==6, Helper.DateTimeFormat(s1.HZQMTM), s1.HZQM));
    return list1;
  }

  SecurityCheckAddFields(BuildContext context) {
    return Container(child: Column(children: getWidgetList(context)));
  }
}
